Imports System.Data.SqlClient

Public Class cls_PDarticuloresultado_

    Implements ICloneable

#Region "Variables ..."
    Private int_idPDarticuloresultado As Integer = 0
    Private int_idPDarticulo As Integer = 0
    Private int_idproveedor As Integer = 0
    Private dbl_cantidad As Double = 0
    Private dbl_valor As Double = 0
    Private int_idunidaddetalle As Integer = 0

    Private obj_proveedor As cls_proveedor_
    Private obj_unidaddetalle As cls_unidaddetalle_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_PDarticuloresultado_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idPDarticuloresultado
        End Get
    End Property
    Property idPDarticuloresultado() As Integer
        Get
            idPDarticuloresultado = int_idPDarticuloresultado
        End Get
        Set(ByVal value As Integer)
            int_idPDarticuloresultado = value
        End Set
    End Property
    Property idPDarticulo As Integer
        Get
            idPDarticulo = int_idPDarticulo
        End Get
        Set(ByVal value As Integer)
            int_idPDarticulo = value
        End Set
    End Property
    Property idproveedor() As Integer
        Get
            idproveedor = int_idproveedor
        End Get
        Set(ByVal value As Integer)
            int_idproveedor = value
        End Set
    End Property
    Property cantidad() As Double
        Get
            cantidad = dbl_cantidad
        End Get
        Set(ByVal value As Double)
            dbl_cantidad = value
        End Set
    End Property
    Property idunidaddetalle() As Integer
        Get
            idunidaddetalle = int_idunidaddetalle
        End Get
        Set(ByVal value As Integer)
            int_idunidaddetalle = value
        End Set
    End Property
    Property valor() As Double
        Get
            valor = dbl_valor
        End Get
        Set(ByVal value As Double)
            dbl_valor = value
        End Set
    End Property
    Property proveedor() As cls_proveedor_
        Get
            If IsNothing(obj_proveedor) Then
                obj_proveedor = New cls_proveedor_
                If Not obj_proveedor.Buscar(idproveedor) Then
                    obj_proveedor = Nothing
                End If
            End If
            proveedor = obj_proveedor
        End Get
        Set(ByVal value As cls_proveedor_)
            obj_proveedor = value
        End Set
    End Property
    Property unidaddetalle() As cls_unidaddetalle_
        Get
            If IsNothing(obj_unidaddetalle) Then
                obj_unidaddetalle = New cls_unidaddetalle_
                If Not obj_unidaddetalle.Buscar(idunidaddetalle) Then
                    obj_unidaddetalle = Nothing
                End If
            End If
            unidaddetalle = obj_unidaddetalle
        End Get
        Set(ByVal value As cls_unidaddetalle_)
            obj_unidaddetalle = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_PDarticuloresultado_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_PDarticuloresultado_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_PDarticuloresultado_)
        While obj_Reader.Read()
            Dim Elemento As New cls_PDarticuloresultado_
            Elemento.int_idPDarticuloresultado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticuloresultado")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticulo"))) = False Then
                Elemento.int_idPDarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idproveedor"))) = False Then
                Elemento.int_idproveedor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idproveedor")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("cantidad"))) = False Then
                Elemento.dbl_cantidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("cantidad")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
                Elemento.dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle"))) = False Then
                Elemento.int_idunidaddetalle = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idPDarticuloresultado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticuloresultado")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticulo"))) = False Then
            int_idPDarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idPDarticulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idproveedor"))) = False Then
            int_idproveedor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idproveedor")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("cantidad"))) = False Then
            dbl_cantidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("cantidad")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
            dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle"))) = False Then
            int_idunidaddetalle = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__Nuevo"
                .Parameters.Add("@idPDarticulo", SqlDbType.Int).Value = int_idPDarticulo
                .Parameters.Add("@idproveedor", SqlDbType.Int).Value = int_idproveedor
                .Parameters.Add("@cantidad", SqlDbType.Decimal).Value = dbl_cantidad
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@idunidaddetalle", SqlDbType.Int).Value = int_idunidaddetalle
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__Modificar"
                .Parameters.Add("@idPDarticuloresultado", SqlDbType.Int).Value = int_idPDarticuloresultado
                .Parameters.Add("@idPDarticulo", SqlDbType.Int).Value = int_idPDarticulo
                .Parameters.Add("@idproveedor", SqlDbType.Int).Value = int_idproveedor
                .Parameters.Add("@cantidad", SqlDbType.Decimal).Value = dbl_cantidad
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@idunidaddetalle", SqlDbType.Int).Value = int_idunidaddetalle
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idPDarticuloresultado As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__Eliminar"
                .Parameters.Add("@idPDarticuloresultado", SqlDbType.Int).Value = idPDarticuloresultado
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idPDarticuloresultado As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__Buscar"
                .Parameters.Add("@idPDarticuloresultado", SqlDbType.Int).Value = idPDarticuloresultado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_PDarticuloresultado_Clon As New cls_PDarticuloresultado_
        obj_PDarticuloresultado_Clon.int_idPDarticuloresultado = Me.int_idPDarticuloresultado
        obj_PDarticuloresultado_Clon.int_idPDarticulo = Me.int_idPDarticulo
        obj_PDarticuloresultado_Clon.int_idproveedor = Me.int_idproveedor
        obj_PDarticuloresultado_Clon.dbl_cantidad = Me.dbl_cantidad
        obj_PDarticuloresultado_Clon.dbl_valor = Me.dbl_valor
        obj_PDarticuloresultado_Clon.int_idunidaddetalle = Me.int_idunidaddetalle
        obj_PDarticuloresultado_Clon.col_Items = Me.col_Items
        Return obj_PDarticuloresultado_Clon
    End Function

    Function CargarXidPDarticulo(ByVal int_idPDarticulo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidPDarticulo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "PDarticuloresultado__CargarXidPDarticulo"
                .Parameters.Add("@idPDarticulo", SqlDbType.Int).Value = int_idPDarticulo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidPDarticulo = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidPDarticulo los registros de PDarticuloresultado_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

End Class
